// pages/it/it.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeModule:null,
        showVipModal: false,
        city: "定位中...", // 默认显示定位中，获取到位置后更新
        latitude: null,
        longitude: null,
        address: "",
        isLocating: false, // 是否正在定位
          liveList: [
            {
              id: 1,
              imageUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.e1dc80b52123994c087255d4708438e1?rik=0LgALA11W3s57Q&riu=http%3a%2f%2fd.ifengimg.com%2fq100%2fimg1.ugc.ifeng.com%2fnewugc%2f20190917%2f16%2fwemedia%2f7d77adea36a2cc04264273bc69810e0e55086c1f_size271_w960_h640.jpg&ehk=9gsjGk2uOiKIDZeksFxSjSjtaJ%2bp05165ZieVFUkFfk%3d&risl=&pid=ImgRaw&r=0',
              tag: '国内游专场',
              time: '7月10日 9:00',
              title: '夏日清凉好去处',
              host: '娜娜',
              desc: '转发直播间赢好礼',
              status: 'live'
            },
            {
              id: 2,
              imageUrl: 'https://img95.699pic.com/photo/50053/0789.jpg_wh300.jpg!/fh/300/quality/90',
              tag: '酒店专场',
              time: '7月10日 10:00',
              title: '江阴国际大酒店',
              host: '小王',
              desc: '舒适住宿体验',
              status: 'live'
            },
            {
              id: 3,
              imageUrl: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.55ZMP2d6wlq2Af7BwbnVvwHaE7?rs=1&pid=ImgDetMain&o=7&rm=3',
              tag: '酒店专场',
              time: '7月10日 11:00',
              title: '同里湖大饭店',
              host: '小李',
              desc: '豪华湖景房推荐',
              status: 'live'
            },
            {
              id: 4,
              imageUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.21d56d864c5dbe3ec879e24261f5e464?rik=zy%2flxX0HP1dy1A&riu=http%3a%2f%2fimgbdb3.bendibao.com%2fcqbdb%2ftour%2f20209%2f10%2f2020910113549_44365.jpg&ehk=0G7rY9fICDvpeTEZjcmNcv73PY3ju5TY3CZff1fL4r0%3d&risl=&pid=ImgRaw&r=0',
              tag: '出境游专场',
              time: '7月11日 9:00',
              title: '爆款返场',
              host: 'Maggie',
              desc: '尾单限时抢购',
              status: 'reservation'
            }
          ],
          // 轮播图图片路径数组
    bannerList: [
        'https://vcg03.cfp.cn/creative/vcg/800/new/VCG211305050246.jpg',
        'https://vcg00.cfp.cn/creative/vcg/800/new/VCG211320993728.jpg',
        'https://vcg01.cfp.cn/creative/vcg/800/new/VCG211428489532.jpg',
        // '/images/haijing.png',
        // '/images/party2.png',
        // '/images/shangwu.png'
      ],
      currentTab: 0, // 当前选中的标签索引
    // 省心跟团数据
    groupItems: [
      {
        id: 1,
        image: "https://ts1.tc.mm.bing.net/th/id/R-C.bda659517a47c3466e35535bfc3ac2d4?rik=TgBIlLoqGjVGvQ&riu=http%3a%2f%2fwww.zjshenxianju.com%2fu%2fcms%2fwww%2f202304%2f27112216m70d.jpg&ehk=uItEi7NLynFulGDSFrrYNYy9kFcPdTth2Ml5khA7Wd0%3d&risl=&pid=ImgRaw&r=0",
        title: "纯玩无购物 | 仙居大神仙居·网红如意桥·龙穿峡3日游",
        desc: "品质团，深度游玩",
        price: 799
      },
      {
        id: 2,
        image: "https://ts2.tc.mm.bing.net/th/id/OIP-C.FeSZNFnmxdQWpMdft-WUoQHaE8?rs=1&pid=ImgDetMain&o=7&rm=3",
        title: "纯玩无购物 | 腾冲、瑞丽、芒市、大理、丽江双飞8日游",
        desc: "品质团，多景点覆盖",
        price: 4799
      },
      {
        id: 3,
        image: "https://ts4.tc.mm.bing.net/th/id/OIP-C.XhUB_JlTpDflmaAH5C-kwgHaE8?rs=1&pid=ImgDetMain&o=7&rm=3",
        title: "纯玩无购物 | 港珠澳双卧8日美食之旅",
        desc: "品质团，美食体验",
        price: 3599
      }
    ],
    // 超值酒店数据
    hotelItems: [
      {
        id: 1,
        image: "https://r.lvyouquan.cn/lyqpic/hold/2017-03-27/image/20170327/20170327114020_4491.jpg",
        title: "豪华海景房",
        desc: "一线海景，尽享奢华",
        price: 1299
      },
      {
        id: 2,
        image: "https://www.jbdzs.net/uploads/210123/2-2101231542551P.jpg",
        title: "精品商务酒店",
        desc: "便捷出行，舒适体验",
        price: 599
      },
      {
        id: 3,
        image: "https://ts1.tc.mm.bing.net/th/id/OIP-C.2PCdsCGOLaYAzoQ2B6MadwHaE7?rs=1&pid=ImgDetMain&o=7&rm=3",
        title: "温泉度假酒店",
        desc: "温泉养生，放松身心",
        price: 899
      }
    ],
    // 旅游度假数据
    resortItems: [
      {
        id: 1,
        image: "https://ts1.tc.mm.bing.net/th/id/OIP-C.TFQPDmrCiQs_1pgMhIkd6QHaE7?rs=1&pid=ImgDetMain&o=7&rm=3",
        title: "三亚阳光沙滩度假",
        desc: "阳光、沙滩、海浪",
        price: 2999
      },
      {
        id: 2,
        image: "https://p6.itc.cn/q_70/images03/20211226/94c12b04e8e245849ae67f2356318d82.jpeg",
        title: "长白山滑雪度假",
        desc: "滑雪胜地，冬季乐园",
        price: 3999
      },
      {
        id: 3,
        image: "https://n.sinaimg.cn/sinakd20117/384/w2048h1536/20200503/dbd3-isyparh5352800.jpg",
        title: "云南大理度假",
        desc: "风花雪月，诗意栖居",
        price: 2599
      }
    ],
    
    // 当季爆款数据
    hotItems: [
      {
        id: 1,
        image: "https://ts4.tc.mm.bing.net/th/id/OIP-C.QSR5KVzTD5dN33K4ZKqXJAHaGN?rs=1&pid=ImgDetMain&o=7&rm=3",
        title: "当季爆款A",
        desc: "限时特惠，不容错过",
        price: 599
      },
      {
        id: 2,
        image: "https://ts2.tc.mm.bing.net/th/id/OIP-C.wiwDTgVS2xKKXhJQPKpFuAHaHa?rs=1&pid=ImgDetMain&o=7&rm=3",
        title: "当季爆款B",
        desc: "热门推荐，口碑之选",
        price: 799
      },
      {
        id: 3,
        image: "https://ts1.tc.mm.bing.net/th/id/OIP-C.-JxTs2cvBBWvE4wTlcizdQHaFP?rs=1&pid=ImgDetMain&o=7&rm=3",
        title: "当季爆款C",
        desc: "新品上市，惊喜不断",
        price: 999
      }
    ],
      cardList: [
        {
          title: "社区广场舞晨练",
          imageUrl: "https://img2.voc.com.cn/2020/07/28/f45550807adbaf43b35cb2455ac2084773db363c1595920474.JPG", // 替换为实际图片路径
          description: "每天清晨一起跳广场舞，强身健体，结识邻里好友。",
          viewCount: 15
        },
        {
          title: "周末红歌合唱",
          imageUrl: "https://ts1.tc.mm.bing.net/th/id/OIP-C.WiqdsRHum3tBQL51-14VSAHaE8?rs=1&pid=ImgDetMain&o=7&rm=3",
          description: "每周合唱经典红歌，重温激情岁月，愉悦身心增感情。",
          viewCount: 28
        },
        {
          title: "手机摄影分享角",
          imageUrl: "https://ts1.tc.mm.bing.net/th/id/OIP-C.ScdEkux05WYpBe51BTbi3wHaE8?rs=1&pid=ImgDetMain&o=7&rm=3",
          description: "用手机记录生活美景，现场交流拍摄技巧，轻松学会晒朋友圈。",
          viewCount: 32
        }
      ],
      postList: [
        {
          id: 1,
          imgUrl: "https://ts4.tc.mm.bing.net/th/id/OIP-C.C_HIQhMn7QEF9470A86ZDgHaEK?rs=1&pid=ImgDetMain&o=7&rm=3", // 本地图片路径，需放到项目的 images 目录；也可填网络图片地址
          title: "工作日去路易威登游轮，预约有点难",
          author: "XY821216",
          likeCount: 63
        },
        {
          id: 2,
          imgUrl: "https://ts3.tc.mm.bing.net/th/id/OIP-C.aRWPDplKNfmQECbLCBYY7AHaHq?rs=1&pid=ImgDetMain&o=7&rm=3",
          title: "一直不懂....",
          author: "人间小...",
          likeCount: 45
        },
        {
          id: 3,
          imgUrl: "https://ts2.tc.mm.bing.net/th/id/OIP-C.J6F-PbYsWm3K_RhrAXjV4wHaE7?rs=1&pid=ImgDetMain&o=7&rm=3",
          title: "周末去哪玩这是个问题",
          author: "儿童成长馆",
          likeCount: 100
        },
        {
          id: 4,
          imgUrl: "https://ts1.tc.mm.bing.net/th/id/OIP-C.YUxmcNdEPvJhWltGVHs6yQHaHa?rs=1&pid=ImgDetMain&o=7&rm=3",
          title: "Labubu家族全科普，这下",
          author: "潮玩达人",
          likeCount: 88
        },
        {
            id: 5,
            imgUrl: "https://ts4.tc.mm.bing.net/th/id/OIP-C.C_HIQhMn7QEF9470A86ZDgHaEK?rs=1&pid=ImgDetMain&o=7&rm=3", // 本地图片路径，需放到项目的 images 目录；也可填网络图片地址
            title: "工作日去路易威登游轮，预约有点难",
            author: "XY821216",
            likeCount: 63
          },
          {
            id: 6,
            imgUrl: "https://ts3.tc.mm.bing.net/th/id/OIP-C.aRWPDplKNfmQECbLCBYY7AHaHq?rs=1&pid=ImgDetMain&o=7&rm=3",
            title: "一直不懂....",
            author: "人间小...",
            likeCount: 45
          },
          {
            id: 7,
            imgUrl: "https://ts2.tc.mm.bing.net/th/id/OIP-C.J6F-PbYsWm3K_RhrAXjV4wHaE7?rs=1&pid=ImgDetMain&o=7&rm=3",
            title: "周末去哪玩这是个问题",
            author: "儿童成长馆",
            likeCount: 100
          },
          {
            id: 8,
            imgUrl: "https://ts1.tc.mm.bing.net/th/id/OIP-C.YUxmcNdEPvJhWltGVHs6yQHaHa?rs=1&pid=ImgDetMain&o=7&rm=3",
            title: "Labubu家族全科普，这下",
            author: "潮玩达人",
            likeCount: 88
          }
      ],
      leftCovers:[
        'https://ts4.tc.mm.bing.net/th/id/OIP-C.bpXEStQz0NgNXl6niN14owHaDt?rs=1&pid=ImgDetMain&o=7&rm=3',
        'https://ts4.tc.mm.bing.net/th/id/OIP-C.56vGSQ6KwQaOvlkfzD7f0wHaHp?rs=1&pid=ImgDetMain&o=7&rm=3',
        'https://image.maigoo.com/upload/images/20180910/09513198958_500x312.jpg_390_240.jpg'
        
      ],
      rightCovers:[
        'https://ts3.tc.mm.bing.net/th/id/OIP-C.6glU-PfM7nnr12jwWkeaAgHaFj?rs=1&pid=ImgDetMain&o=7&rm=3',
        'https://ts1.tc.mm.bing.net/th/id/R-C.86f60de4927aeac6f1d21ceaac877d60?rik=r%2bKyu%2b6VNCyehw&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50036%2f0196.jpg_wh1200.jpg&ehk=uQVHtyvwTiUJOr%2fw6YslqS5v5B7J%2fhpXAYsAna950Pk%3d&risl=&pid=ImgRaw&r=0',
        'https://ts1.tc.mm.bing.net/th/id/R-C.997caa0e9cfd90398d6dfc8af7c795e0?rik=X8ZYx%2fA9G0PIbA&riu=http%3a%2f%2fn.sinaimg.cn%2fsinacn09%2f650%2fw870h580%2f20180721%2f85f0-hfqtahi0948182.jpg&ehk=mofJ%2fGr0l0WKOXIxVfOt%2bEAtG8bmmS8nD2V2o2vzEdk%3d&risl=&pid=ImgRaw&r=0'
      ]
    
  
    },
    goToactivity(){
        wx.navigateTo({
          url: '/pages/activities/activities',
        })
    },

    // 在Page对象中添加以下方法
    
    /**
     * 跳转到朋友圈详情页
     */
    goToPostDetail: function(e) {
      // 获取当前点击的post数据
      const postData = e.currentTarget.dataset.post;
      
      // 跳转到详情页并传递数据
      wx.navigateTo({
        url: `/pages/post-detail/post-detail?postData=${JSON.stringify(postData)}`
      });
    },
    
    /**
     * 返回上一页
     */
    navigateBack: function() {
      wx.navigateBack();
    },
    
    // 获取实时定位
    getLocation() {
      // 防止重复点击
      if (this.data.isLocating) {
        return;
      }

      this.setData({
        isLocating: true,
        city: "定位中..."
      });

      // 检查位置权限
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userLocation'] === false) {
            // 用户拒绝过位置权限，引导用户手动开启
            this.showLocationAuthModal();
          } else {
            // 获取位置信息
            this.startLocation();
          }
        },
        fail: () => {
          this.startLocation();
        }
      });
    },

    // 开始定位
    startLocation() {
      wx.getLocation({
        type: 'gcj02', // 使用国测局坐标系
        isHighAccuracy: true, // 开启高精度定位
        highAccuracyExpireTime: 3000, // 超时时间，单位ms
        success: (res) => {
          console.log('定位成功：', res);
          this.setData({
            latitude: res.latitude,
            longitude: res.longitude
          });
          
          // 进行逆地理编码获取地址信息
          this.reverseGeocoder(res.latitude, res.longitude);
        },
        fail: (err) => {
          console.error('定位失败：', err);
          this.handleLocationError(err);
        },
        complete: () => {
          this.setData({
            isLocating: false
          });
        }
      });
    },

    // 逆地理编码
    reverseGeocoder(latitude, longitude) {
      // 由于微信小程序的逆地理编码需要特殊权限，这里使用模拟数据
      // 实际项目中可以接入第三方地图API服务
      
      // 模拟城市数据（根据经纬度范围判断大致位置）
      let cityName = '未知城市';
      if (latitude >= 39.4 && latitude <= 41.1 && longitude >= 115.7 && longitude <= 117.4) {
        cityName = '北京市';
      } else if (latitude >= 30.7 && latitude <= 31.9 && longitude >= 120.9 && longitude <= 122.2) {
        cityName = '上海市';
      } else if (latitude >= 22.4 && latitude <= 23.6 && longitude >= 113.0 && longitude <= 114.5) {
        cityName = '广州市';
      } else if (latitude >= 22.2 && latitude <= 22.8 && longitude >= 113.8 && longitude <= 114.6) {
        cityName = '深圳市';
      } else if (latitude >= 30.2 && latitude <= 30.8 && longitude >= 120.0 && longitude <= 120.8) {
        cityName = '杭州市';
      } else if (latitude >= 31.1 && latitude <= 32.4 && longitude >= 118.6 && longitude <= 119.2) {
        cityName = '南京市';
      } else if (latitude >= 29.4 && latitude <= 30.7 && longitude >= 104.0 && longitude <= 104.9) {
        cityName = '成都市';
      } else if (latitude >= 34.2 && latitude <= 34.8 && longitude >= 108.8 && longitude <= 109.1) {
        cityName = '西安市';
      } else {
        // 如果不在预设范围内，显示经纬度
        cityName = `${latitude.toFixed(2)}, ${longitude.toFixed(2)}`;
      }
      
      this.setData({
        city: cityName,
        address: `经度: ${longitude.toFixed(4)}, 纬度: ${latitude.toFixed(4)}`
      });

      // 保存到本地存储
      wx.setStorageSync('userLocation', {
        city: this.data.city,
        latitude: latitude,
        longitude: longitude,
        address: this.data.address,
        timestamp: Date.now()
      });

      wx.showToast({
        title: '定位成功',
        icon: 'success'
      });
    },

    // 处理定位错误
    handleLocationError(err) {
      console.error('定位错误：', err);
      
      let errorMsg = '定位失败';
      if (err.errMsg.includes('auth deny')) {
        errorMsg = '请开启位置权限';
      } else if (err.errMsg.includes('timeout')) {
        errorMsg = '定位超时';
      } else if (err.errMsg.includes('unsupported')) {
        errorMsg = '设备不支持定位';
      }

      this.setData({
        city: errorMsg
      });

      wx.showToast({
        title: errorMsg,
        icon: 'none'
      });
    },

    // 显示位置权限引导弹窗
    showLocationAuthModal() {
      wx.showModal({
        title: '需要位置权限',
        content: '为了提供更好的服务，需要获取您的位置信息。请在设置中开启位置权限。',
        confirmText: '去设置',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            wx.openSetting({
              success: (settingRes) => {
                if (settingRes.authSetting['scope.userLocation']) {
                  // 用户开启了位置权限，重新定位
                  this.startLocation();
                }
              }
            });
          }
        }
      });
    },

    // 刷新定位
    refreshLocation() {
      this.getLocation();
    },

    // 切换标签
  switchTab(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    });
  },

  // swiper切换时触发
  swiperChange(e) {
    this.setData({
      currentTab: e.detail.current
    });
  },

    goToLiveStreaming() {
        // 跳转到常用旅客页面
        wx.switchTab({
          url: '/pages/LiveStreaming/LiveStreaming'
        });
      },
    goToReservation() {
        // 跳转到预约页面
        wx.navigateTo({
          url: '/pages/reservation/reservation'
        });
      },
      goTogoodReview() {
        // 跳转到预约页面
        wx.navigateTo({
          url: '/pages/goodReview/goodReview'
        });
      },
      goTopromotion(){
          wx.navigateTo({
            url: '/pages/promotion/promotion',
          })
      },
      
      /* 打开弹窗 */
    openVipModal() {
        this.setData({ showVipModal: true });
      },
    
      /* 关闭弹窗 */
      closeVipModal() {
        this.setData({ showVipModal: false });
      },
    
      /* 跳转到入会页面 */
      goJoinVip() {
        this.closeVipModal();          // 先关弹窗
        wx.navigateTo({
          url: '/pages/vip/join'      // 确保有这个页面
        });
      },
    
      /* 阻止冒泡，防止点内容区关闭弹窗 */
      stopBubble() {},
 
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      // 页面加载时尝试获取位置
      this.getLocation();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      // 页面显示时检查是否需要刷新位置
      const lastLocation = wx.getStorageSync('userLocation');
      if (lastLocation && Date.now() - lastLocation.timestamp > 300000) { // 5分钟刷新一次
        this.getLocation();
      }
    },

    // 模块点击事件（新增）
  handleModuleClick: function(e) {
    const moduleId = e.currentTarget.dataset.id;
    this.setData({
      activeModule: moduleId
    });
    
    // 可添加跳转逻辑
    wx.navigateTo({
      url: `/pages/${moduleId}/${moduleId}`
    });
  },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
  
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
      // 下拉刷新时重新定位
      this.getLocation();
      wx.stopPullDownRefresh();
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
  
    }
  })